package MapReduce.Demo5_PhoneTrafficCountByPhoneNum;

import MapReduce.writableBean.PhoneTrafficWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

/**
 * @Author lixinlei
 * @Date 2023/3/20 9:15
 */
public class PhoneTrafficCountPartitioner extends Partitioner<Text, PhoneTrafficWritable> {

    @Override
    public int getPartition(Text text, PhoneTrafficWritable phoneTrafficWritable, int numPartitions) {

        int backNum = 0;

        //把key转成字符串类型的手机号
        String phoneNum = text.toString();

        //截取手机号的前三位作为手机号段，分区的判断条件
        int phoneHeader = Integer.parseInt(phoneNum.substring(0, 3));

        if(phoneHeader>=130 && phoneHeader<150){
            backNum = 0;
        }else if(phoneHeader>=150 && phoneHeader<170){
            backNum = 1;
        }else if(phoneHeader>=170 && phoneHeader<=199){
            backNum = 2;
        }else{
            backNum = 3;
        }

        return backNum;
    }
}
